package com.community.service;

import com.community.dao.NoticeDao;
import com.community.dao.impl.NoticeDaoImpl;
import com.community.model.Notice;
import java.util.List;

public class NoticeService {
    ////通过NoticeDaoImpl类的构造函数创建了一个NoticeDaoImpl类的实例，并将其赋值给noticeDao变量。
    private final NoticeDao noticeDao = new NoticeDaoImpl();

    public void publishNotice(Notice notice) {
        validateNotice(notice);
        noticeDao.insert(notice);
    }
    public void deleteNotice(int id) {
        noticeDao.delete(id);
    }

    public List<Notice> getAllNotices() {
        return noticeDao.getAll();
    }


    // 获取单个公告的方法
    public Notice getNoticeById(int id) {
        if (id <= 0) {
            throw new IllegalArgumentException("公告ID必须为正整数");
        }
        return noticeDao.getById(id);
    }

    private void validateNotice(Notice notice) {
        if (notice.getTitle() == null || notice.getTitle().trim().isEmpty()) {
            throw new IllegalArgumentException("公告标题不能为空");
        }
        if (notice.getContent() == null || notice.getContent().trim().isEmpty()) {
            throw new IllegalArgumentException("公告内容不能为空");
        }
    }
}